import redis





"""
要求: 使用redis来做用户表
    1.存入账户密码
    2.用户新建之后,id自增
    3.可以查看到所有的用户
    4.可以统计用户的数量
"""

# 表结构
"""
1.hash
表名:user:{id}
fields: name:xxx password:xxxx

2.有序集合
表名 user_set
score->id  value->hash的表名

3.string:
表名 user_id
value id号 每次增值1


"""


def new_user():
    r = redis.StrictRedis(host="127.0.0.1", port=6379)
    # script = """
    #     local user_id = redis.call("incr", KEYS[1])
    #     local hash_table_name = "user:"..user_id
    #     redis.call("zadd","user_set",user_id,hash_table_name)
    #     redis.call("hmset",hash_table_name,"name",user_id)
    # """
    # script1 = r.register_script(script)
    # x = script1(keys=["user_id"], args=[])
    # print(x)
    with r.pipeline() as p:
        for i in range(100000):
            user_id = p.incr('user_id')
            hash_table_name = "user:{}".format(user_id)
            #p.zadd('user_set', user_id, hash_table_name)
            #p.hmset(hash_table_name, {"name": "user:{}".format(user_id), 'password': 'rimiaaa{}'.format(user_id)})
        p.execute()


if __name__ == "__main__":
    new_user()
